package generatedClasses;

import java.util.*;
import struts.*;
import struts.tools.*;

public class ServletActionCreator
{
  protected static String nl;
  public static synchronized ServletActionCreator create(String lineSeparator)
  {
    nl = lineSeparator;
    ServletActionCreator result = new ServletActionCreator();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "";
  protected final String TEXT_2 = NL;
  protected final String TEXT_3 = NL;
  protected final String TEXT_4 = NL;
  protected final String TEXT_5 = NL;
  protected final String TEXT_6 = NL;
  protected final String TEXT_7 = NL;
  protected final String TEXT_8 = NL;
  protected final String TEXT_9 = NL;
  protected final String TEXT_10 = NL;
  protected final String TEXT_11 = NL;
  protected final String TEXT_12 = NL;
  protected final String TEXT_13 = NL;
  protected final String TEXT_14 = NL;
  protected final String TEXT_15 = NL;
  protected final String TEXT_16 = NL + "\t";
  protected final String TEXT_17 = NL;
  protected final String TEXT_18 = NL;
  protected final String TEXT_19 = NL;
  protected final String TEXT_20 = NL + "\t\t";
  protected final String TEXT_21 = NL;
  protected final String TEXT_22 = NL + "\t\t" + NL + "\t\t";
  protected final String TEXT_23 = "\tfinal String ";
  protected final String TEXT_24 = " = lForm.get(";
  protected final String TEXT_25 = ");" + NL + "\t";
  protected final String TEXT_26 = NL + "\t\t" + NL + "\t\t";
  protected final String TEXT_27 = NL;
  protected final String TEXT_28 = NL;
  protected final String TEXT_29 = NL;
  protected final String TEXT_30 = NL + "\t";
  protected final String TEXT_31 = NL + "\t\t\t";
  protected final String TEXT_32 = ",";
  protected final String TEXT_33 = NL + "\t\t\t";
  protected final String TEXT_34 = "));" + NL + "\t\t\t" + NL + "\t\t";
  protected final String TEXT_35 = NL;
  protected final String TEXT_36 = NL;
  protected final String TEXT_37 = NL;
  protected final String TEXT_38 = NL;
  protected final String TEXT_39 = NL;
  protected final String TEXT_40 = NL;
  protected final String TEXT_41 = NL;
  protected final String TEXT_42 = NL;
  protected final String TEXT_43 = NL;
  protected final String TEXT_44 = NL;
  protected final String TEXT_45 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(TEXT_1);
    stringBuffer.append( "package struts.servletAction;" );
    stringBuffer.append(TEXT_2);
    stringBuffer.append(" import javax.servlet.http.HttpServletRequest;");
    stringBuffer.append(TEXT_3);
    stringBuffer.append(" import javax.servlet.http.HttpServletResponse;");
    stringBuffer.append(TEXT_4);
    stringBuffer.append(" import org.apache.struts.Globals;");
    stringBuffer.append(TEXT_5);
    stringBuffer.append(" import org.apache.struts.action.Action;");
    stringBuffer.append(TEXT_6);
    stringBuffer.append(" import org.apache.struts.action.ActionForm;");
    stringBuffer.append(TEXT_7);
    stringBuffer.append(" import org.apache.struts.action.ActionForward;");
    stringBuffer.append(TEXT_8);
    stringBuffer.append(" import org.apache.struts.action.ActionMapping;");
    stringBuffer.append(TEXT_9);
    stringBuffer.append(" import org.apache.struts.action.ActionMessage;");
    stringBuffer.append(TEXT_10);
    stringBuffer.append(" import org.apache.struts.action.ActionMessages;");
    stringBuffer.append(TEXT_11);
    stringBuffer.append(" import org.lip6.struts.actionForm.AddContactValidationForm;");
    stringBuffer.append(TEXT_12);
    stringBuffer.append(" import org.lip6.struts.domain.DAOContact;");
    stringBuffer.append(TEXT_13);
     ArrayList<DataElement> formElts = (ArrayList<DataElement>) argument;
    stringBuffer.append(TEXT_14);
    stringBuffer.append(TEXT_15);
    stringBuffer.append("public class AddContactAction extends Action {");
    stringBuffer.append(TEXT_16);
    stringBuffer.append(TEXT_17);
    stringBuffer.append("	public ActionForward execute(final ActionMapping pMapping,");
    stringBuffer.append(TEXT_18);
    stringBuffer.append("			ActionForm pForm, final HttpServletRequest pRequest,");
    stringBuffer.append(TEXT_19);
    stringBuffer.append("			final HttpServletResponse pResponse) {");
    stringBuffer.append(TEXT_20);
    stringBuffer.append(TEXT_21);
    stringBuffer.append("		final AddContactValidationForm lForm=(AddContactValidationForm)pForm;");
    stringBuffer.append(TEXT_22);
    	for(DataElement de:formElts){ 
    stringBuffer.append(TEXT_23);
    stringBuffer.append(de.getName().replaceAll("[ ]",""));
    stringBuffer.append(TEXT_24);
    stringBuffer.append(de.getName().replaceAll("[ ]",""));
    stringBuffer.append(TEXT_25);
    }
    stringBuffer.append(TEXT_26);
    stringBuffer.append(TEXT_27);
    stringBuffer.append("		// create a new Contact");
    stringBuffer.append(TEXT_28);
    stringBuffer.append("		final DAOContact lDAOContact = new DAOContact();");
    stringBuffer.append(TEXT_29);
    stringBuffer.append("		final String lError = lDAOContact.addContact(");
    stringBuffer.append(TEXT_30);
     for(int i=0;i<formElts.size()-1;i++){ 
    stringBuffer.append(TEXT_31);
    stringBuffer.append(formElts.get(i).getName().replaceAll("[ ]",""));
    stringBuffer.append(TEXT_32);
    }
    stringBuffer.append(TEXT_33);
    stringBuffer.append(formElts.get(formElts.size()-1).getName().replaceAll("[ ]",""));
    stringBuffer.append(TEXT_34);
    stringBuffer.append(TEXT_35);
    stringBuffer.append("	if(lError == null) {");
    stringBuffer.append(TEXT_36);
    stringBuffer.append("		// if no exception is raised,  forward \"success\"");
    stringBuffer.append(TEXT_37);
    stringBuffer.append("		return pMapping.findForward(\"success\");");
    stringBuffer.append(TEXT_38);
    stringBuffer.append("	}");
    stringBuffer.append(TEXT_39);
    stringBuffer.append("	else {");
    stringBuffer.append(TEXT_40);
    stringBuffer.append("		// If any exception, return the \"error\" forward");
    stringBuffer.append(TEXT_41);
    stringBuffer.append("		return pMapping.findForward(\"error\");");
    stringBuffer.append(TEXT_42);
    stringBuffer.append("	}");
    stringBuffer.append(TEXT_43);
    stringBuffer.append("   }");
    stringBuffer.append(TEXT_44);
    stringBuffer.append("}");
    stringBuffer.append(TEXT_45);
    return stringBuffer.toString();
  }
}
